package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.config.AppConfig;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.SysSettingDto;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.utils.RedisUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/16 15:28
 **/
@RestController
@RequestMapping("/admin")
public class AdminSettingController extends ABaseController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private AppConfig appConfig;

    @GlobalInterceptor
    @RequestMapping("/getSysSetting")
    public ResponseVO getSysSetting() {
        SysSettingDto sysSetting = redisUtil.getSysSetting();
        return getSuccessResponseVO(sysSetting);
    }

    @GlobalInterceptor(checkAdmin = true)
    @RequestMapping("/saveSysSetting")
    public ResponseVO saveSysSetting(SysSettingDto sysSettingDto,
                                     MultipartFile robotFile,
                                     MultipartFile robotCover) {

        if (robotFile != null) {
            String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
            File targetFile = new File(baseFolder + Constants.FILE_FOLDER_AVATAR_NAME);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            String filePath = targetFile.getPath() + "/" + Constants.ROBOT_UID + Constants.IMAGE_SUFFIX;
            try {
                robotFile.transferTo(new File(filePath));
                robotCover.transferTo(new File(filePath + Constants.COVER_IMAGE_SUFFIX));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        redisUtil.saveSysSetting(sysSettingDto);
        return getSuccessResponseVO(null);
    }

}
